package payment.simulator.institution.tx.unionsdk;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import payment.api.tx.unionsdk.Tx7020Request;

/**
 * <pre>
 * Modify Information: 获取会话AppToken
 * Author       Date          Description
 * ============ ============= ============================
 * caohc        2018-11-01    Create this file
 * </pre>
 */

public class Tx7020 extends HttpServlet {

    private static final long serialVersionUID = 2716197312423114644L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.获取参数
            String institutionID = request.getParameter("InstitutionID");
            String appID = request.getParameter("AppID");
            String userID = request.getParameter("UserID");

            // 创建交易请求对象
            Tx7020Request tx7020Request = new Tx7020Request();

            tx7020Request.setInstitutionID(institutionID);
            tx7020Request.setAppID(appID);
            tx7020Request.setUserID(userID);

            // 3.执行报文处理
            tx7020Request.process();

            // 4.将参数放置到request对象
            
            request.setAttribute("plainText", tx7020Request.getRequestPlainText());
            request.setAttribute("message", tx7020Request.getRequestMessage());
            request.setAttribute("signature", tx7020Request.getRequestSignature());            
            request.setAttribute("txCode", "7020");
            request.setAttribute("txName", "获取会话AppToken");  
            request.setAttribute("action", request.getContextPath() + "/SendMessage");

            // 5.转向Request.jsp页面
            request.getRequestDispatcher("/Request.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            processException(request, response, e.getMessage());
        }
    }
    
    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
        try {
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        } catch (Exception e) {
        }
    }
}
